package com.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bean.*;
import com.dao.BlogSQL;
import com.dao.CommentSQL;

/**
 * Servlet implementation class CommentEdit
 */
@WebServlet("/CommentEdit")
public class CommentEdit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int bid=Integer.parseInt(req.getParameter("bid"));
		HttpSession session=req.getSession();
		ArrayList<Comment> coms=new CommentSQL().findByBlog(bid);
		session.setAttribute("comment", coms);
		//req.getRequestDispatcher("comment.jsp").forward(req, resp);
	}

	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		User user=(User)req.getSession().getAttribute("pass");
		int uid=user.getId();
		int bid=Integer.parseInt(req.getParameter("bid"));
		String param=req.getParameter("ccid");
		int ccid=Integer.parseInt(param==""?"0":param);//回复用户的评论
		String title=req.getParameter("title");
		String content=req.getParameter("content");
		Comment com=new Comment(title, content, uid, bid,ccid);
		System.out.println("tttttttttt");
		if(new CommentSQL().writeComment(com)){
			System.out.println(content);
			resp.getWriter().write("发表成功");
		//Blog blog=BlogSQL.find(bid);
		//req.setAttribute("blog", blog);
		//req.getRequestDispatcher("read.jsp").forward(req, resp);
		}
	}

}
